package com.china08.yxyapi.api.manage.tec.teaching.zujuan.intellcomponent;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.china08.yxyapi.api.YxyApiBaseController;
import com.china08.yxyapi.entity.db.res.YxyResBookContentsKnoledges;
import com.china08.yxyapi.model.YxyApiPageReqModel;
import com.china08.yxyapi.model.YxyApiRespModel;
import com.china08.yxyapi.repository.db.YxyResBookContentsKnoledgesRepository;
import com.china08.yxyapi.service.manage.tec.teaching.zujuan.IntellComponentQuestionService;
import com.china08.yxyapi.util.YxyResUtil;

/**
 * 教育局平台办公 智能组卷 按章节
 * 
 */
//@Api(tags = { "v50", "智能组卷    按章节" })
@CrossOrigin
@RestController
public class IntellComponentChapterQuestionCountController extends YxyApiBaseController {
	@Autowired
	private IntellComponentQuestionService intellComponentQuestionService;
	@Autowired
	private YxyResUtil yxyResUtil;

	@Autowired
	private YxyResBookContentsKnoledgesRepository yxyResBookContentsKnoledgesRepository;
	
//	@ApiOperation(value = "智能组卷    按章节")
//	@ResponseBody
	@RequestMapping(value = "/api_yw/manage/tec/teaching/zujuan/intellchaptercomponent/questioncount", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
	public RespModel precess(HttpServletRequest request, HttpServletResponse response, @RequestBody ReqModel reqModel) {
		RespModel model=new RespModel();
		// 所选的章节id
		List<String> bookContentIdList = reqModel.bookContentIdList;
		// 题的难易程度
		String difficultyLevel1 = reqModel.difficultyLevel;
		
		List<String> bookContent=new ArrayList<String>();
		for (String string : bookContentIdList) {
			if (string!=null && !string.equals("")) {
				bookContent.add(string);
			}
		}
		List<Long> allCount=intellComponentQuestionService.getQuestionTypeAllCount(bookContent, difficultyLevel1);
		
		//求章节下面对应的所有知识点id
		List<String> bookcontentListAll = new ArrayList<String>();
		for (String bc : bookContentIdList) {
			if(StringUtils.isBlank(bc)){
				continue;
			}
			List<String> bookcontentList = yxyResUtil.findTocByTocParent(bc);
			bookcontentListAll.addAll(bookcontentList);
		}
        List<YxyResBookContentsKnoledges> bookContentsKnoledges = null;
        List<Integer> knowIds = new ArrayList<Integer>();
        if (bookcontentListAll.size()>0) {			
        	bookContentsKnoledges = yxyResBookContentsKnoledgesRepository.findByBookContentIdIn(bookcontentListAll);
        	 if (bookContentsKnoledges.size() > 0) {
                 knowIds = bookContentsKnoledges.stream().map(x -> x.getKnowledgeId())
                         .collect(Collectors.toList());
             }
		}
		model.singleCount=allCount.get(0);
		model.moreCount=allCount.get(1);
		model.blanksCount=allCount.get(2);
		model.answerCount=allCount.get(3);
		//知识点id
		model.knowIds=knowIds;
		return model;
	}

	protected static class RespModel extends YxyApiRespModel {
		//单选题的数量
        public Long singleCount;
        //多选题的数量
        public Long moreCount;
        //填空题的数量
        public Long blanksCount;
        //解答题的数量
        public Long answerCount;
        //章节下面的所有知识点id
        public List<Integer> knowIds;
	}

	protected static class ReqModel extends YxyApiPageReqModel {
		// 章节id
		public List<String> bookContentIdList;
		// 题的难易程度
		public String difficultyLevel;
	}
}
